package com.zlzkj.app.aop;

import java.lang.reflect.Method;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zlzkj.app.model.User;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.After;
//import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.zlzkj.app.aop.Log;
import com.zlzkj.app.model.Operation;
import com.zlzkj.app.service.OperationService;

@Aspect
@Component
public class LogAop_1 {
	
	@Autowired
	private OperationService operationService;

	ThreadLocal<Long> time=new ThreadLocal<Long>();
	ThreadLocal<String> tag=new ThreadLocal<String>();
	
	/**
	 * 在所有标注@Log的地方切入
	 * @param joinPoint
	 */
	@Before("@annotation(com.zlzkj.app.aop.Log)")
	public void beforeExec(JoinPoint joinPoint){
		
		time.set(System.currentTimeMillis());
		tag.set(UUID.randomUUID().toString());
		
		HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();  
        HttpSession session =request.getSession();  
        //String person = session.getAttribute("CUR_USER");
        org.apache.shiro.subject.Subject my = SecurityUtils.getSubject();
        User user = (User) my.getPrincipal();
        String username = user.getUsername();
		//info(joinPoint);
		MethodSignature ms=(MethodSignature) joinPoint.getSignature();
		Method method=ms.getMethod();
		String methodName = method.getAnnotation(Log.class).name();
		Operation operation = new Operation();
		operation.setPerson(username);
		operation.setOperation(methodName);
		operation.setContent(username+"用户进行了一次"+methodName+"操作");
//        System.out.println(operation.getId()+"//"+operation.getPerson());
//        System.out.println(operation.getOperation()+"//"+operation.getContent());
//        System.out.println(operation.getTime());
		operationService.save(operation);
	}

	//+"标记"+tag.get()
//	@After("@annotation(com.zlzkj.app.aop.Log)")
//	public void afterExec(JoinPoint joinPoint){
//		MethodSignature ms=(MethodSignature) joinPoint.getSignature();
//		Method method=ms.getMethod();
//		System.out.println("标记为"+tag.get()+"的方法"+method.getName()+"运行消耗"+(System.currentTimeMillis()-time.get())+"ms");
//	}
	
//	@Around("@annotation(com.zlzkj.app.aop.Log)")
//	public void aroundExec(ProceedingJoinPoint pjp) throws Throwable{
//		System.out.println("我是Around，来打酱油的");
//		pjp.proceed();
//	}
	
//	private void info(JoinPoint joinPoint){
//		System.out.println("--------------------------------------------------");
//		System.out.println("King:\t"+joinPoint.getKind());
//		System.out.println("Target:\t"+joinPoint.getTarget().toString());
//		Object[] os=joinPoint.getArgs();
//		System.out.println("Args:");
//		for(int i=0;i<os.length;i++){
//			System.out.println("\t==>参数["+i+"]:\t"+os[i].toString());
//		}
//		System.out.println("Signature:\t"+joinPoint.getSignature());
//		System.out.println("SourceLocation:\t"+joinPoint.getSourceLocation());
//		System.out.println("StaticPart:\t"+joinPoint.getStaticPart());
//		System.out.println("--------------------------------------------------");
//	}

}
